# --------------- < import > ---------------
from chiori import Config, Logger, Event
from .main import VkBackend
import asyncio
# --------------- < import > ---------------
chat_const = int(2E9)

class VkUser(VkBackend):
	def __init__(self, app):
		super(VkUser, self).__init__()
		self.name = 'vk_user'
		self.backend_type = ["cli", "vk_cli"]
		self.l = Logger('VkUser')
		self.app = app
		self.c = Config('vk_user', {'token':'token'})
		self.__dict__.update(self.c.group_data)
		self.api_url = "https://api.vk.com/method/{{}}?access_token={}&v={}" \
			.format(self.token, self.version)

	async def start(self):
		await self.app.startEvent()
		res = await self.request('users.get')
		if not res["error"]:
			self.__dict__.update(res["response"][0])
		else:
			self.l.error('не удалось получить данные пользователя')
			await self.app.stopEvent()

		while True:
			c = input(f'[{self.first_name} {self.last_name}]> ')
			await self.app.messageEvent(await self.convert('/'+c))


	def setLevel(self, event, c):
		event.model['level'] = 10
		return event


	async def convert(self, text):
		e = Event()
		e.set('text', text)
		e.set('from.id', self.id)
		e.set('from.first_name', self.first_name)
		e.set('from.last_name', self.last_name)

		return e


	def decorateMessage(self, message):
		message = str(message)
		decorators = {'<e>':'<ошибка>', '<c>':'<выполнено>',
					  '<m>':'<непонимание>'}
		for k, v in decorators.items():
			message = message.replace(k, v)

		return message


	async def message(self, event, message, **params):
		print(message)
